package ooday04;

import ooday05.StaticMethod;

public class PolymorphicDemo {
    public static void main(String[] args) {
        /*
        Dog o1 = new Dog("小黑",2,"黑");
        o1.eat();
        Animal o2 = new Dog("大白",2,"白");
        o2.drink();
        o2.eat();
        Swim o3 = new Dog("小黄",2,"黄");
         */

        //演示向上造型(多态)的第一点应用
        /*
        Master master = new Master();
        Dog dog = new Dog("小黑",1,"黑");
        Chick chick = new Chick("小鸡",2,"黄");
        Fish fish = new Fish("小金",1,"金");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);
         */

        //演示向上造型(多态)的第2点应用：
        //Animal o new Animal[5];  //编译错误，抽象类不能被实例化
        /*
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("小黑",2,"黑"); //向上造型
        animals[1] = new Dog("大白",2,"白");
        animals[2] = new Chick("花鸡",3,"花");
        animals[3] = new Chick("小鸡",1,"黄");
        animals[4] = new Fish("小金",2,"金");
        for (int i=0;i<animals.length;i++){  //遍历所有动物
            animals[i].eat();
            animals[i].drink();
            if (animals[i] instanceof Dog){
                Dog a = (Dog) animals[i];
                a.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick b = (Chick) animals[i];
                b.layEggs();
            }
            if (animals[i] instanceof Swim){
                Swim c = (Swim) animals[i];
                c.swim();
            }
        }


        //演示强转成功的条件：
        /*
        Animal o = new Dog("小黑",2,"黑");
        Dog a = (Dog) o;  //引用o所指向的对象，就是Dog类型
        Swim b = (Swim) o;  //引用o所指的对象，实现了Swim接口
        //Fish c = (Fish) o;  //运行时会发生ClassCastException类型转换异常

        System.out.println(o instanceof Dog);  //true
        System.out.println(o instanceof Swim);  //true
        System.out.println(o instanceof Fish);  //false
        if(o instanceof Fish){
            Fish c = (Fish) o;
        }else {
            System.out.println("o没有继承Fish类，也没有实现Fish接口");
        }
         */
    }
 }
